package com.bt.att;

import com.bt.att.dao.PhoneDao;
import com.bt.att.model.Call;
import com.bt.att.model.Message;
import com.bt.att.model.Phone;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * TableDataScrollPane.
 *
 * @author Zakir Magdum
 */
public class DetailsTablePane extends JScrollPane {
    private static final DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ssaa");
    private JTable table;
    private String[] columnNames = {"Time",
            "Number",
            "Duration",
            "Incoming?"};
    private String[] messagesColumnNames = {"Time",
            "Number",
            "SizeKB",
            "Incoming?"};
    private Object[][] data = {};

    @Autowired
    private PhoneDao dao;

    public void init() {
        table = new JTable(new DetailsTableModel(columnNames, data));
        setViewportView(table);
    }

    public void setCalls(List<Call> calls) {
        data = new Object[calls.size()][columnNames.length];
        for (int ii = 0; ii < calls.size(); ii++) {
            Call call = calls.get(ii);
            data[ii][0] = dateFormat.format(call.getTimestamp());
            data[ii][1] = call.getTo().getNumber();
            data[ii][2] = call.getDuration();
            data[ii][3] = call.isIncoming();
        }
        table.setModel(new DetailsTableModel(columnNames, data));
    }

    public void setMessages(List<Message> messages) {
        data = new Object[messages.size()][columnNames.length];
        for (int ii = 0; ii < messages.size(); ii++) {
            Message message = messages.get(ii);
            data[ii][0] = dateFormat.format(message.getTimestamp());
            data[ii][1] = message.getTo().getNumber();
            data[ii][2] = message.getMsgKB();
            data[ii][3] = message.isIncoming();
        }
        table.setModel(new DetailsTableModel(messagesColumnNames, data));
    }


    class DetailsTableModel extends AbstractTableModel {
        private String[] columnNames;
        private Object[][] data;

        public DetailsTableModel(String[] columnNames, Object[][] data) {
            this.columnNames = columnNames;
            this.data = data;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
    }

}
